﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace REVISIONMAX30
{
    public partial class frmSourceDirectory : Form
    {
        public frmSourceDirectory()
        {
            InitializeComponent();
        }

        public UIHandler uiHandler { get; set; }
        public DataObjects.SourceDirectory sourceDirectory { get; set; }
        public DataObjects.Group group { get; set; }

        public void View(UIHandler pUIHandler, DataObjects.SourceDirectory pChildObject, DataObjects.Group pParentObject)
        {
            this.uiHandler = pUIHandler;
            this.sourceDirectory = pChildObject;
            this.group = pParentObject;
            this.UpdateViewer();
            base.ShowDialog();
        }

        public Guid DataObjectGUID
        {
            get { return this.sourceDirectory.DataObjectGUID; }
        }

        private void UpdateObject()
        {
            this.sourceDirectory.DirectoryName = this.txtDirectoryName.Text;
            this.sourceDirectory.ScanDirectoryRecusivly = this.chkScanDirectoryRecusivly.Checked;
        }

        private void UpdateViewer()
        {
            this.txtDirectoryName.Text = this.sourceDirectory.DirectoryName;
            this.chkScanDirectoryRecusivly.Checked = this.sourceDirectory.ScanDirectoryRecusivly.BoolValue();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            this.Save(true);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Save(false);
        }

        public void Save(bool pApply)
        {
            UpdateObject();

            if (!group.SourceDirectoryList.Exists(t => t.DataObjectGUID == this.DataObjectGUID))
            {
                group.SourceDirectoryList.Add(this.sourceDirectory);
            }

            if (!pApply)
            {
                this.Close();
            }
        }

        private void txtDirectoryName_TextChanged(object sender, EventArgs e)
        {
            this.Text = "Directory Name: " + this.txtDirectoryName.Text;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (System.IO.Directory.Exists(this.txtDirectoryName.Text))
                this.folderBrowserDialog1.SelectedPath = this.txtDirectoryName.Text;
            else
                this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;

            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtDirectoryName.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }
         
    }
}
